Completed
Push — master ( d6eb80...a478e7 )
by Muhammad Dyas
15s queued 12s
created

utils.js ➔ extractMessageAndConfig   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
1
/**
2
 * @param {array} names - list of names
3
 * @param {integer} winnerCount - total winner that picked from names
0 ignored issues
show
Documentation introduced by
The parameter winnerCount does not exist. Did you maybe forget to remove this comment?
Loading history...
4
 * @returns {array} - list of winner
5
 */
6
export function getRandomWinners(names, winnerCount = 1) {
7
  for (let i = names.length - 1; i > 0; i--) {
8
    const j = Math.floor(Math.random() * (i + 1));
9
    [names[i], names[j]] = [names[j], names[i]];
0 ignored issues
show
Bug introduced by
The variable names seems to be never declared. If this is a global, consider adding a /** global: names */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable j seems to be never declared. If this is a global, consider adding a /** global: j */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable i seems to be never declared. If this is a global, consider adding a /** global: i */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
  }
11
  return names.slice(0, winnerCount);
12
}
13
14
/**
15
 * @param {string} string - text that will be extracted
16
 * @returns {array} list of extracted text
17
 */
18
export function extractMessageByDoubleQuote(string) {
19
  const regex = /"[^"]+"/g;
20
  const extracted = string.match(regex);
21
  if (extracted) {
22
    return extracted.map((s) => s.replace(/"(.+)"/, '$1'));
23
  }
24
  return [];
25
}
26
27
/**
28
 * @param {string} string - text that will be extracted
29
 * @returns {array} list of extracted text
30
 */
31
export function extractMessageAndConfig(string) {
32
  const regex = /"[^"]+"|[^\s]+/g;
33
  return string.match(regex).map((s) => s.replace(/"(.+)"/, '$1'));
34
}
35
36
/**
37
 * Config means anything else beside string inside quote
38
 * @param {string} argumentText -
39
 * @param {array} messages - extracted messages
40
 * @returns {array} extracted command
41
 */
42
export function extractConfig(argumentText, messages) {
43
  const extractedTextCommands = extractMessageAndConfig(argumentText);
44
  return extractedTextCommands.filter((val) => !messages.includes(val));
45
}
46